#pragma once
#include<iostream>
#include<pthread.h>
#include<mutex>

//封装锁
class Mutex
{
public:
    //构造
    Mutex()
    {
        //调用锁的初始化函数进行初始化
        pthread_mutex_init(&_lock,nullptr);
    }
    //上锁
    void Lock()
    {
        pthread_mutex_lock(&_lock);
    }
    //解锁
    void Unlock()
    {
        pthread_mutex_unlock(&_lock);
    }

    ~Mutex()
    {
        pthread_mutex_destroy(&_lock);
    }
    
private:
    pthread_mutex_t _lock;
};

//还要再封装一层.只要创建这个对象就自动上锁，出了作用域调用析构函数就自动解锁
class LockGuard
{
public:
    LockGuard(Mutex* _mutex):_mutexp(_mutex)
    {
        _mutexp->Lock();
    }
    ~LockGuard()
    {
        _mutexp->Unlock();
    }

private:
    Mutex* _mutexp;
};
